package com.ruoyi.web.controller.project;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.project.domain.AmProject;
import com.ruoyi.project.service.IAmProjectService;
import com.ruoyi.project.domain.vo.AmProjectQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 项目管理Controller
 * 
 * @author ruoyi
 * @date 2020-07-28
 */
@RestController
@RequestMapping("/project/project")
@Api(description = "项目管理")
public class AmProjectController extends BaseController
{
    @Autowired
    private IAmProjectService amProjectService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('project:project:list')")
    @GetMapping("/list")
    @ApiOperation("查询项目列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectName", value = "项目名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "deptId", value = "所属机构Id", required = false, dataType = "Long", paramType = "query")
            })
    public TableDataInfo list(AmProjectQueryVo amProject)
    {
        startPage();
        List<AmProject> list = amProjectService.selectAmProjectList(amProject);
        return getDataTable(list);
    }

    /**
     * 导出项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('project:project:export')")
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出项目管理列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectName", value = "项目名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "deptId", value = "所属机构Id", required = false, dataType = "Long", paramType = "query")
    })
    public AjaxResult export(AmProjectQueryVo amProject)
    {
        List<AmProject> list = amProjectService.selectAmProjectList(amProject);
        ExcelUtil<AmProject> util = new ExcelUtil<AmProject>(AmProject.class);
        return util.exportExcel(list, "project");
    }


    @GetMapping("/importTemplate")
    @ApiOperation("导入模板")
    public AjaxResult importTemplate()
    {
        ExcelUtil<AmProject> util = new ExcelUtil<AmProject>(AmProject.class);
        return util.importTemplateExcel("项目数据");
    }


    @Log(title = "项目管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('project:project:import')")
    @PostMapping("/importData")
    @ApiOperation("导入接口")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        if(null == file || !file.getOriginalFilename().contains("xls")){
            return AjaxResult.error("格式不支持");
        }

        ExcelUtil<AmProject> util = new ExcelUtil<AmProject>(AmProject.class);
        List<AmProject> projects = util.importExcel(file.getInputStream());
        if (null == projects || projects.size() == 0 ){
            return AjaxResult.error("请不要上传空文件");
        }
        List<String> list = amProjectService.selectAllAmProjectList();

        for (int i = 0 ; i< projects.size() ; i++){
            AmProject project = projects.get(i);
            if (null == project){
                return AjaxResult.error("第"+(i+2)+"行表格数据不正确,请检查后重新导入");
            } else  if ( StringUtils.isEmpty(project.getProjectName()) ){
                return AjaxResult.error("第"+(i+2)+"行项目名称数据缺失,请检查后在重新导入");
            }else if( StringUtils.isEmpty( project.getAddress())){
                return AjaxResult.error("第"+(i+2)+"行项目地址数据缺失,请检查后在重新导入");
            }else if(StringUtils.isNull(project.getArea()) ){
                return AjaxResult.error("第"+(i+2)+"行项目面积数据缺失,请检查后在重新导入");
            } else if(StringUtils.isEmpty(project.getProperty())){
                return AjaxResult.error("第"+(i+2)+"行物业公司数据缺失,请检查后在重新导入");
            }else if(  StringUtils.isNull( project.getPropertyMoney())){
                return AjaxResult.error("第"+(i+2)+"行物业费数据缺失,请检查后在重新导入");
            }else if( StringUtils.isEmpty( project.getParking()) ){
                return AjaxResult.error("第"+(i+2)+"行停车位数量数据缺失,请检查后在重新导入");
            }else if( StringUtils.isEmpty(project.getDeliveryStandardValue())){
                return AjaxResult.error("第"+(i+2)+"行交付标准数据缺失,请检查后在重新导入");
            }else if(  StringUtils.isEmpty( project.getProjectType())){
                return AjaxResult.error("第"+(i+2)+"行项目类型数据缺失,请检查后在重新导入");
            }else if( StringUtils.isNull(project.getStandardLayerArea() )){
                return AjaxResult.error("第"+(i+2)+"行标准层面积数据缺失,请检查后在重新导入");
            }else if(  StringUtils.isNull( project.getStandardLayerHeight())){
                return AjaxResult.error("第"+(i+2)+"行楼层层高数据缺失,请检查后在重新导入");
            }else if(  StringUtils.isEmpty( project.getIntroduction() )) {
                return AjaxResult.error("第"+(i+2)+"行项目简介数据缺失,请检查后在重新导入");
            }else if (list.contains(project.getProjectName())){
                return AjaxResult.error("第"+(i+2)+"行项目名称已存在,请修改后在重新导入");
            }

        }
        LoginUser loginUser = tokenService.getLoginUser();
        String message = amProjectService.importProject(projects, updateSupport,loginUser);
        return AjaxResult.success(message);
    }
    /**
     * 获取项目管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:project:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取项目管理详细信息")
    @ApiImplicitParam(name = "id", value = "项目Id", required = true, dataType = "Long", paramType = "query")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(amProjectService.selectAmProjectById(id));
    }

    /**
     * 新增项目管理
     */
    @PreAuthorize("@ss.hasPermi('project:project:add')")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增项目管理")
    public AjaxResult add(@RequestBody AmProject amProject)
    {
        return toAjax(amProjectService.insertAmProject(amProject));
    }

    /**
     * 修改项目管理
     */
    @PreAuthorize("@ss.hasPermi('project:project:edit')")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改项目管理")
    @ApiImplicitParam(example = "ddd")
    public AjaxResult edit(@RequestBody AmProject amProject)
    {
        return toAjax(amProjectService.updateAmProject(amProject));
    }

    /**
     * 删除项目管理
     */
    @PreAuthorize("@ss.hasPermi('project:project:remove')")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	@ApiOperation("删除项目管理")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(amProjectService.deleteAmProjectByIds(ids));
    }

/*
    */
/**
     * 项目树
     * @return
     *//*

    @GetMapping("/tree")
    @ApiOperation("项目树")
    public AjaxResult getProjectTree(){
        return AjaxResult.success(amProjectService.getProjectTree());
    }
*/

    /**
     * 项目树
     * @return
     */
    @GetMapping("/tree/{commissionId}")
    @ApiOperation("获取佣金绑定规则项目树")
    public AjaxResult getProjectTree(@PathVariable Long commissionId){
        return AjaxResult.success(amProjectService.getProjectTree(commissionId).get(0).get("projectTree"));
    }

    /**
     * 项目楼宇树
     * @return
     */
    @GetMapping("/buildingTree")
    @ApiOperation("项目楼宇树")
    public AjaxResult getProjectBuildingTree(){
        return AjaxResult.success(amProjectService.getProjectBuildingTree());
    }

    /**
     * 项目列表
     * @return
     */
    @GetMapping("/alllist")
    @ApiOperation("项目列表")
    public AjaxResult alllist(){
        return AjaxResult.success(amProjectService.list());
    }
}
